package com.example.edupublisher.controller;

import com.example.edupublisher.bean.TradeProvinceFinalAmount;
import com.example.edupublisher.bean.TradeProvinceOrderCt;
import com.example.edupublisher.service.TradeStatsService;
import com.example.edupublisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName: TradeController
 * Package: com.atguigu.edu.publisher.controller
 * Description:
 *
 * @Author Mr.2
 * @Create 2023/9/12 11:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/edu/realtime/trade")
public class TradeController {
    // 自动装载
    @Autowired
    private TradeStatsService tradeStatsService;

    // 1. 省份 下单数
    @RequestMapping("/provinceOrderCt")
    public String getProvinceOrderCt(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }

        List<TradeProvinceOrderCt> tradeProvinceOrderCtList = tradeStatsService.getTradeProvinceOrderCt(date);

        if (tradeProvinceOrderCtList == null) {
            return "";
        }

        StringBuilder mapData = new StringBuilder("[");

        for (int i = 0; i < tradeProvinceOrderCtList.size(); i++) {

            TradeProvinceOrderCt tradeProvinceOrderCt = tradeProvinceOrderCtList.get(i);
            String provinceName = tradeProvinceOrderCt.getProvinceName();
            Integer orderCt = tradeProvinceOrderCt.getOrderCt();

            mapData.append("{\n" +
                    "        \"name\": \"" + provinceName + "\",\n" +
                    "        \"value\": " + orderCt + "\n" +
                    "      }");

            // 中间分隔符
            if (i < tradeProvinceOrderCtList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "    \"valueName\": \"订单数\"\n" +
                "  }\n" +
                "}";
    }


    // 2. 省份 下单金额
    @RequestMapping("/provinceFinalAmount")
    public String getProvinceFinalAmount(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }

        List<TradeProvinceFinalAmount> tradeProvinceFinalAmountList = tradeStatsService.getTradeProvinceFinalAmount(date);
        if (tradeProvinceFinalAmountList == null) {
            return "";
        }

        StringBuilder mapData = new StringBuilder("[");

        for (int i = 0; i < tradeProvinceFinalAmountList.size(); i++) {
            TradeProvinceFinalAmount tradeProvinceFinalAmount = tradeProvinceFinalAmountList.get(i);
            String provinceName = tradeProvinceFinalAmount.getProvinceName();
            Double finalAmount = tradeProvinceFinalAmount.getFinalAmount();

            mapData.append("{\n" +
                    "        \"name\": \"" + provinceName + "\",\n" +
                    "        \"value\": " + finalAmount + "\n" +
                    "      }");
            // 中间分隔符
            if (i < tradeProvinceFinalAmountList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }

        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "    \"valueName\": \"订单金额\"\n" +
                "  }\n" +
                "}";

    }

    // 3. 某天 下单总金额统计
    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        BigDecimal gmv = tradeStatsService.getGMV(date);

        String json = "{\"status\": 0,\"msg\": \"\",\"data\": " + gmv + "}";
        return json;
    }

}
